package com.james.customcamera.listener;

import android.content.ClipData;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by James on 2018/3/4 0004.
 */

public abstract class onRecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
    private GestureDetectorCompat mGestureDetectorCompat;
    private RecyclerView mRecyclerView;

    public onRecyclerItemClickListener(RecyclerView recyclerView){
        mRecyclerView = recyclerView;
        mGestureDetectorCompat  = new GestureDetectorCompat(mRecyclerView.getContext(),new ItemTouchHelperGestureListener());
    }
    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        mGestureDetectorCompat.onTouchEvent(e);
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        mGestureDetectorCompat.onTouchEvent(e);
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }

    public abstract void onItemClick(RecyclerView.ViewHolder viewHolder);
    public abstract void onItemLongClick(RecyclerView.ViewHolder viewHolder);
    public class ItemTouchHelperGestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            View childViewUnder  = mRecyclerView.findChildViewUnder(e.getX(),e.getY());
            if (childViewUnder!=null){
                RecyclerView.ViewHolder childViewHolder
                        =mRecyclerView.getChildViewHolder(childViewUnder);
                onItemClick(childViewHolder);
            }
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            View childViewUnder  = mRecyclerView.findChildViewUnder(e.getX(),e.getY());
            if (childViewUnder!=null){
                RecyclerView.ViewHolder childViewHolder
                        =mRecyclerView.getChildViewHolder(childViewUnder);
                onItemLongClick(childViewHolder);
            }

        }
    }

}
